Different programming languages directly support different basic data types. Many programming languages allow additional user defined data types created out of basic data types. For example, arrays, structures and records.
Data objects or variables have to be represented using a finite state machine such as computer memory, which is a sequence of binary digits. Therefore integer data types supported by programming languages often have a finite range. Programming languages often define the range of a particular data type as the number of binary digits or bits that should be used to represent a value belonging to that data type. This range of a data type is represented as data type width. For example data type “short” in the Java programming language is defined to have a data type width of 16 bits giving a range of values −65536, −65535 . . . 0 . . . +65535.
Some programming languages do not define the data type width for basic data types, and chose to leave the decision of defining these to compiler implementors. This means that different compilers may consider different data type width for the same data type. For example, in programming languages C and C++ valid data widths for the ‘int’ includes 16-bits, 32-bits or 64-bits. Such data types therefore may have different widths depending on the compiler used.
Complex programs developed using high level programming languages are often organized into smaller units as given below.                a) Functions or procedures with a defined set of formal parameters such as a list of data types. When invoked with a set of actual arguments that is a list of matching data values or objects, these functions perform some set of operations on those values and/or objects possibly invoking other functions to complete the operations.        b) User defined data types that define more complex data types using other existing basic or user defined data types.        
Often, the same operation such as swap or sorting may be defined for different data types. Without generic programming, the programmer is forced to develop different functions for implementing the same operation for formal parameters with different data types. For example a sort function for an array of integers and another for an array of floating point numbers.
Similarly, a complex data type such as a linked list or complex number can be defined using different basic data types. For example, one user defined data type that can be used for integer real and imaginary parts and another for floating point real and imaginary parts. Generic programming allows the development of functions and complex data types that can be used with more than one set of formal parameter types or constant data values. For example a single sort function for an array of integers, an array of floating point numbers or an array of any data type that defines an ordering relationship such as “less than” between any two values in the domain of the data type. Different languages and compilers may implement generic programming in different ways. The two common ways are static resolution of type parameters and dynamic resolution of type parameters. A generic function or type provides an implementation of the function or type, which can be used with different sets of the type parameters. However, a programmer may sometime choose to over-ride the implementation of the generic function or type, for a specific set of type parameters. This is referred to as specialization, and such types or functions are referred to as specialized type for function.
An example in C++ is given below:
/ / Generic function swap.Template<class C>void swap (C & c1, C&c2) {C tmp;tmp = c1;c1 = c2;c2 = tmp;}// specialized function swap for swapping 2strings.// will only work when length of strings isequal.// swaps string , instead of swappingpointers to strings.template <> void swap (char * C1, char * c2){char tmp;While (*c1 && *c2){tmp = *c1;*c1 = *c2;*c2 = tmp;} /* while */}